package cn.david.basic.service.impl;

import cn.david.basic.mapper.BasicMapper;
import cn.david.basic.query.BasicQuery;
import cn.david.basic.service.IBasicService;
import cn.david.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BasicServiceImpl<T> implements IBasicService<T> {


    @Autowired
    private BasicMapper<T> basicMapper;
    @Override
    @Transactional
    public void add(T t) {
        basicMapper.save(t);

    }

    @Override
    @Transactional
    public void upadte(T t) {
        basicMapper.update(t);

    }

    @Override
    @Transactional
    public void del(Long id) {

        basicMapper.remove(id);

    }

    @Override
    public T getById(Long id) {
        return basicMapper.loadById(id);
    }

    @Override
    public List<T> getAll() {
        return basicMapper.loadAll();
    }

    @Override
    public PageList<T> queryData(BasicQuery query) {
        Long total = basicMapper.queryCount(query);
        if (total==null || total.intValue()<1){
            return new PageList<>();
        }
        List<T> list = basicMapper.queryData(query);

        return new PageList<>(total,list);

    }


}
